﻿using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using Grinder.Exif;
using Grinder.Renamer;

namespace Grinder.Renamer.RenamePlugins
{
    internal sealed class JpegRenamePlugin : IRenamePlugin
    {
        private readonly Dictionary<string, string> _tokens;

        public IEnumerable<string> Extensions { get { return new List<string> { "*.JPEG", "*.JPG" }; } }
        public string Name { get { return "JPEG files"; } }
        public Dictionary<string, string> Tokens { get { return _tokens; } }
        public Image Icon { get { return Properties.Resources.pictures; } }
        public override string ToString() { return string.Format("{0} ({1})", Name, string.Join(",", Extensions.ToArray()).ToLower()); }

        public JpegRenamePlugin()
        {
            _tokens = new Dictionary<string, string>();

            foreach (var tag in SupportedTags.Instance)
            {
                _tokens.Add(tag.FieldName.ToLower(), tag.Description);
            }
        }

        public Dictionary<string, object> GetTokens(FileInfo file)
        {
            var dic = new Dictionary<string, object>();

            var tags = new ExifTags(file.FullName);

            foreach (var key in ExifTags.SupportedTagNames)
            {
                var val = tags.ContainsKey(key) ? tags[key].Value : "";

                dic.Add(key.ToLower(), val);
            }

            return dic;
        }
    }
}
